package com.wuzf.mall.loading

import android.content.Context
import android.support.v7.app.AppCompatDialog
import android.view.Gravity
import com.blankj.utilcode.util.ScreenUtils
import com.wang.avi.AVLoadingIndicatorView
import com.wuzf.mall.R


object MallLoader {

    private val list = arrayListOf<AppCompatDialog>()

    private val defaultLoader = LoadStyles.BallSpinFadeLoaderIndicator

    private fun createDialog(context: Context?, loadView: AVLoadingIndicatorView): AppCompatDialog {
        // 创建Dialog对象
        var dialog = AppCompatDialog(context, R.style.dialog)
        val screenWidth = ScreenUtils.getScreenWidth()
        val screenHeight = ScreenUtils.getScreenHeight()
        // 设置
        dialog.setContentView(loadView)
        val attribute = dialog.window?.attributes
        if (attribute != null) {
            attribute.width = screenWidth / 5
            attribute.height = screenHeight / 5
            attribute.height = attribute.height + screenHeight / 6
            attribute.gravity = Gravity.CENTER
        }
        list.add(dialog)
        return dialog
    }

    fun createLoader(context: Context?, type: LoadStyles? = defaultLoader) {
        val avLoadingIndicatorView = AVLoadingIndicatorView(context)
        avLoadingIndicatorView.setIndicator(type?.name)
        // 创建dialog并显示
        createDialog(context, avLoadingIndicatorView).show()
    }

    fun stopLoading() {
        for (index in list) {
            if (index.isShowing) {
                // 如果正在显示，就关闭掉
                index.cancel()
            }
        }
        list.clear()
    }
}
